package com.guoq.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoq.constant.Configration;
import com.guoq.mapper.OrderItemMapper;
import com.guoq.mapper.OrdersMapper;
import com.guoq.pojo.EUDataGridResult;
import com.guoq.pojo.OrderItem;
import com.guoq.pojo.Orders;
import com.guoq.pojo.OrdersExample;
import com.guoq.pojo.OrdersExample.Criteria;
import com.guoq.pojo.Result;
import com.guoq.pojo.User;
import com.guoq.service.IOrderService;
import com.guoq.utils.DateConvert;
import com.guoq.utils.IDUtils;

/**
 * 
 * <P>类名称:OrderServiceImpl
 * <P>类描述: 订单service 
 * <P>创建人:郭强  
 * <P>修改人:XXX 
 * <P>修改时间:2017年7月22日 下午4:23:23
 * <P>修改备注: 
 * @version 1.0.0
 * Copyright(c)2017西部在线-版权所有
 */
@Service("orderService")
public class OrderServiceImpl implements IOrderService {

	@Resource
	private OrdersMapper orderMapper;
	@Resource
	private OrderItemMapper orderItemMapper;

	/**
	 * 根据id查询商品信息
	 */
	@Override
	public Orders getById(int id) {
		// TODO Auto-generated method stub
		return this.orderMapper.selectByPrimaryKey(id);
	}

	/**
	 * 获取不分页列表
	 */
	@Override
	public List<Orders> getList() {
		// TODO Auto-generated method stub
		OrdersExample example = new OrdersExample();
		Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo(0);//正常
		example.setOrderByClause("createtime desc");
		List<Orders> list = orderMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取3个月内数据列表（不分页）
	 */
	@Override
	public EUDataGridResult getThreeMonthList(String userid) {
		
		// TODO Auto-generated method stub
		OrdersExample example = new OrdersExample();
		Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo(0);//正常
		criteria.andCustIdEqualTo(userid);
		criteria.andCreatetimeBetween(DateConvert.getTimeByMonth(-Configration.ORDER_THREE), new Date());
		example.setOrderByClause("createtime desc");
		List<Orders> list = orderMapper.selectByExample(example);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		return result;
	}
	
	/**
	 * 获取3个月之前数据列表（不分页）
	 */
	@Override
	public EUDataGridResult getThreeMonthAgoList(String userid) {
		
		// TODO Auto-generated method stub
		OrdersExample example = new OrdersExample();
		Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo(0);//正常
		criteria.andCustIdEqualTo(userid);
		criteria.andCreatetimeLessThan(DateConvert.getTimeByMonth(-Configration.ORDER_THREE));
		example.setOrderByClause("createtime desc");
		List<Orders> list = orderMapper.selectByExample(example);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		return result;
	}
	
	/**
	 * 待评价订单数据列表（不分页）
	 */
	@Override
	public EUDataGridResult waitEvaluateList(String userid) {
		
		// TODO Auto-generated method stub
		OrdersExample example = new OrdersExample();
		Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo(Configration.ORDER_STATUS_SUCCESS);//订单状态-交易成功
		criteria.andIsEvaluateEqualTo(Configration.ORDER_EVALUATE_ALREADY);//订单评价状态-待评价
		criteria.andCustIdEqualTo(userid);
		example.setOrderByClause("close_time desc");
		List<Orders> list = orderMapper.selectByExample(example);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		return result;
	}
	
	/**
	 * 取消订单数据列表（不分页）
	 */
	@Override
	public EUDataGridResult cancelList(String userid) {
		
		// TODO Auto-generated method stub
		OrdersExample example = new OrdersExample();
		Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo(Configration.ORDER_STATUS_CANCEL);//订单状态-交易取消
		criteria.andCustIdEqualTo(userid);
		example.setOrderByClause("cancel_time desc");
		List<Orders> list = orderMapper.selectByExample(example);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		return result;
	}

	/**
	 * 根据顾客id获取分页列表
	 */
	@Override
	public EUDataGridResult getListByCustId(Integer page, Integer rows, String custid) {
		
		OrdersExample example = new OrdersExample();
		Criteria criteria = example.createCriteria();
		criteria.andCustIdEqualTo(custid);
		//分页处理
		PageHelper.startPage(page, rows);
		List<Orders> list = orderMapper.selectByExample(example);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		//取记录总条数
		PageInfo<Orders> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}
	
	/**
	 * 根据顾客id获取分页列表
	 */
	@Override
	public EUDataGridResult getListByShopId(Integer page, Integer rows, String shopid) {
		
		OrdersExample example = new OrdersExample();
		Criteria criteria = example.createCriteria();
		criteria.andShopIdEqualTo(shopid);
		//分页处理
		PageHelper.startPage(page, rows);
		List<Orders> list = orderMapper.selectByExample(example);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		//取记录总条数
		PageInfo<Orders> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}
	
	@Override
	public List<Orders> selectByExample(OrdersExample example) {
		// TODO Auto-generated method stub
		List<Orders> list = orderMapper.selectByExample(example);
		return list;
	}

	@Override
	public int countByExample(OrdersExample example) {
		// TODO Auto-generated method stub
		int count = orderMapper.countByExample(example);
		return count;
	}

	@Override
	public Result create(Map<String, String[]> map, User bean) throws Exception {
		
		Orders order = new Orders();
		OrderItem orderItem = new OrderItem();
		
		String[] shop_id = map.get("shop_id");	//商铺id
		String[] item_id = map.get("item_id");	//商品id
		String[] pay_type = map.get("pay_type");	//支付方式
		String[] payment = map.get("payment");	//实付金额
		String[] post_fee = map.get("post_fee");	//配送费用
		String[] name = map.get("name");		//订单商品名称
		String[] num = map.get("num");			//订单商品数量
		String[] price = map.get("price");		//订单商品价格
		
		String order_id = IDUtils.genOrderId();	//自动生成的订单id
		if(shop_id.length > 0){
			order.setOrderId(order_id);
			order.setShopId(shop_id[0]);
			order.setCustId(bean.getUserId());
			order.setPayType(Integer.parseInt(pay_type[0]));
			order.setPayment(Long.parseLong(payment[0]));
			order.setPostFee(Long.parseLong(post_fee[0]));
		}
		
		Result result = this.insertOrder(order);
		if(result.getStatus() != 200){
			throw new Exception();
		}else{
			
			Date currenttime = new Date();
			
			for(int i=0; i<name.length; i++){
				orderItem.setOrderId(order_id);
				orderItem.setItemId(item_id[i]);
				orderItem.setName(name[i]);
				orderItem.setNum(Integer.parseInt(num[i]));
				orderItem.setPrice(Long.parseLong(price[i]));
				orderItem.setTotalFee(Long.parseLong(price[i])*Long.parseLong(num[i]));
				orderItem.setCreatetime(currenttime);
				orderItem.setUpdatetime(currenttime);
				orderItemMapper.insertSelective(orderItem);
			}
		}
		return Result.ok();
	}
	
	/**
	 * 添加商品描述
	 *<P>开发者:郭强
	 *<P>方法名: insertItemDesc
	 *<P>描述: 
	 *@param itemId
	 *@param desc
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	private Result insertOrder(Orders bean){
		
		bean.setStatus(0);
		bean.setCreatetime(new Date());
		bean.setUpdatetime(new Date());
		orderMapper.insertSelective(bean);
		return Result.ok();
	}

	/**
	 * 删除
	 */
	@Override
	public Result delete(Orders bean) throws Exception {

		Result result = null;
		
		bean.setStatus(2);
		bean.setUpdatetime(new Date());
		int num = orderMapper.updateByPrimaryKeySelective(bean);
		
		if(num == 1){
			result = Result.ok();
		}else {
			result = Result.build(500,"error");
			throw new Exception();
		}
		
		return result;
	}
	
	/**
	 * 根据order_id删除订单
	 */
	@Override
	public Result deleteByOrderId(String orderId) throws Exception {
		
		Result result = null;
		Orders record = new Orders();
		record.setOrderId(orderId);;
		OrdersExample example = new OrdersExample();
		Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo(Configration.ORDER_STATUS_DELETE);
		int num = orderMapper.updateByExampleSelective(record, example);
		
		if(num == 1){
			result = Result.ok();
		}else {
			result = Result.build(500,"error");
			throw new Exception();
		}
		
		return result;
	}

	/**
	 * 编辑
	 */
	@Override
	public Result update(Orders bean) throws Exception {

		Result result = null;
		int num = orderMapper.updateByPrimaryKeySelective(bean);
		
		if(num == 1){
			result = Result.ok();
		}else {
			result = Result.build(500,"error");
			throw new Exception();
		}
		
		return result;
	}

}
